package com.atom.plugin.flow

import com.android.build.gradle.AppPlugin
import com.atom.plugin.core.Log
import com.atom.plugin.core.isTrue
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.api.plugins.PluginContainer

class FlowPlugin : Plugin<Project> {
    protected lateinit var project: Project
    protected lateinit var flowExtension: FlowExtension
    override fun apply(project: Project) {
        Log.init(project)
        this.project = project
        val plugins: PluginContainer = project.plugins
        // 主要是负责检查当前的plugin是否在android环境使用
        val hasAppPlugin: Boolean = plugins.hasPlugin(AppPlugin::class.java)
        hasAppPlugin.isTrue {
            val extensions: ExtensionContainer = project.extensions
            this.flowExtension = extensions.create("FlowExtension", FlowExtension::class.java)
                ?: FlowExtension()
            project.afterEvaluate { p ->
                Log.setEnable(this.flowExtension.enableUse)
                Log.setAbort(this.flowExtension.abortError)
                Log.w("FlowPlugin 1 - 2 $flowExtension")
            }
            Log.w("FlowPlugin 0 - 2 $flowExtension")
        }
    }
}